//
//  main.m
//  OC6_省市县限时代码2
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    NSString *path = @"/Users/dllo/Desktop/DLS151034/OC6_字典&集合/OC6_字典&集合课后作业/OC6_作业练习/省市区/area.txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *arr = [str componentsSeparatedByString:@"\n"];
    
    NSMutableArray *proArr = [NSMutableArray array];
    NSMutableArray *cityArr = nil;
    NSMutableArray *areaArr = nil;
    
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *temp = [arr objectAtIndex:i];
        if (![temp hasPrefix:@" "]) {
            cityArr = [NSMutableArray array];
            NSMutableDictionary *proDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:temp, @"provinceName", cityArr, @"city", nil];
            [proArr addObject:proDic];
        } else if (![temp hasPrefix:@"   "]){
            areaArr = [NSMutableArray array];
            NSMutableDictionary *cityDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:temp, @"cityName", areaArr, @"area", nil];
            [cityArr addObject:cityDic];
        } else {
            [areaArr addObject:temp];
        }
        [temp release];
    }

    NSData *data = [NSJSONSerialization dataWithJSONObject:proArr options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *str1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", str1);
    
    
    return 0;
}
